<?php
namespace app\admin\controller;
use think\Db;
class C extends Base
{
    //删除
    public function del(){
        if( is_post() ){
            $d = input();
            $t = $d['t'];
            
            if( !isset($d['id']) || !$d['id'] ){
                com_error(['缺少条件']);
            }else{
                Db::name($t)->delete( $d['id'] );
            }
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
    //批量删除
    public function delall($t=''){
        if( is_post() ){
            $ids = input('ids/a');
            if($ids){
                Db::name($t)->delete( $ids );
            }else{
                com_error(['缺少条件']);
            }
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }
    
    //编辑
    public function edit(){
        if( is_post() ){
            $d = input();
            $t = $d['t'];
            unset($d['t']);
            if(!isset($d['id']) || !$d['id']){
                com_check(['缺少条件']);
            }else{
                Db::name($t)->update($d);;
            }
            $config = [
                'suc' => [
                    'msg' => '编辑成功！',
                    'js_action'=>"alert",
                ],
            ];
            com_check($config);
        }
    }

    //复制一条数据
    public function copy(){
        if( is_post() ){
            $d = input();
            $t  = $d['t'];
            $id = $d['id'];
            unset($d['t']);
            $rs = Db::name($t)->find( $id );
            if(isset($rs['title']))
                $rs['title'] = $rs['title']."-拷贝";
            if(isset($rs['cat_title']))
                $rs['cat_title'] = $rs['cat_title']."-拷贝";
            unset($rs['id']);
            Db::name($t)->insert($rs);
    
            $config = [
                'suc' => [
                    'msg' => '复制成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
    
    //上传文件
    public function up($dir='', $f='', $thumb=1){
        if( is_post() ){
            if( !isset($_FILES['file']) || in_array($_FILES['file']['type'], ['application/php','application/javascript'])){
                com_error(['上传文件错误!']);
                com_check();
            }else{
                if(in_array($_FILES['file']['type'], ['image/png', 'image/gif', 'image/jpeg','image/x-icon'])){
                    $thumb = $thumb ? true : false;
                    $path = com_up_pic($dir, 'file', $thumb);
                }else{
                    $path = com_up_file($dir, 'file');
                }
                $config = [
                    'data' => [
                        'path' => $path,
                        'f'=> $f,
                    ],
                ];
                com_check($config);
            }
        }
    }
    
    //编辑器上传
    public function editup($dir=''){
        if( is_post() )
            $d = [
                'code' =>0,
                'msg' =>0,
                'data' =>[
                    'src' =>'',
                    'title' =>'',  
                ],
            ];
            if( !isset($_FILES['file']) || in_array($_FILES['file']['type'], ['application/php','application/javascript'])){
                $d['msg'] = '上传文件错误!';
            }else{
                if(in_array($_FILES['file']['type'], ['image/png', 'image/gif', 'image/jpeg','image/x-icon'])){
                    $path = com_up_pic($dir, 'file', false);
                }else{
                    $path = com_up_file($dir, 'file');
                }
                $d['data']['src'] = "/".$path;
                $arr = explode(".", $_FILES['file']['name']);
                $ext = end($arr);
                $d['data']['title'] = str_replace(".".$ext, "", $_FILES['file']['name']);
            }
            exit(json_encode($d));
    }

    //相册通用操作++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //上传图片
    public function addphoto(){
        if( is_post() ){
    
            $path = com_up_pic('photo', 'input-file', false);
            $data = [
                'path' => $path,
                'initialPreview' => ["<img src='/".$path."' style='width:auto;height:160px;' class='kv-preview-data file-preview-image'>",],
                'initialPreviewConfig' => [
                    [
                        'url'       => url('ajax/removephoto'),
                        'key'       => $path,
                    ]
                ],
            ];
            exit(json_encode($data, JSON_UNESCAPED_UNICODE ));
        }
    }
    
    //删除图片
    public function removephoto(){
        if( is_post() ){
            $file = input('key');
            @unlink ($file);
            com_check();
        }
    }
    //不删除图片，但前端需要json格式
    public function notremovephoto(){
        com_check();
    }
}